 #define  _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
#include<assert.h>
#include<stdlib.h>
void Initqueue(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void Destroy(Queue* pq)
{
	assert(pq);
	assert(!(ifempty(pq)));
	Qnode* cur = pq->phead;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
}

bool ifempty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}



void Queuepush(Queue* pq, Quetype x)
{
	assert(pq);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc error");
		return;
	}
	newnode->next = NULL;
	newnode->data = x;
	if (pq->phead == NULL)
	{
		assert(pq->ptail == NULL);
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void Queuepop(Queue* pq)
{
	assert(pq);
	assert(!(ifempty(pq)));
	if (pq->phead->next == NULL)
	{
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		Qnode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}

Quetype Queuetop(Queue* pq)
{
	assert(pq);
	assert(!(ifempty(pq)));
	return pq->phead->data;


	//Quetype top = pq->phead->data;
	//pq->phead = pq->phead->next;
	//return top;

}

Quetype	Queueback(Queue* pq)
{
	assert(pq);
	assert(!(ifempty(pq)));
	return pq->ptail->data;
}

//void Print(Queue* pq)
//{
//	assert(pq);
//	Qnode* cur = pq->phead;
//	while (cur)
//	{
//		Qnode* next = cur->next;
//		printf("%d", cur->data);
//		cur = next;
//	}
//	printf("\n");
//}

int	Queuesize(Queue* pq)
{
	assert(pq);
	return pq->size;
}